package com.doublefish.doublefishrobot.ui.home.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.doublefish.doublefishrobot.R;
import com.doublefish.doublefishrobot.ui.home.bean.ModeSettingBean;
import com.doublefish.doublefishrobot.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者:dugaofeng on 2024/10/23 16:09
 */
public class ModeCustomLoopDialog extends Dialog implements View.OnClickListener{

    private ModeSettingItemLoopContentView loopContentView;
    private TextView submitTxt;
    private TextView cancelTxt;

    private Context mContext;
    private ModeSettingBean.ModeSettingLoopBean modeSettingLoopBean;
    public OnCloseListener listener;

    public ModeCustomLoopDialog(Context context, ModeSettingBean.ModeSettingLoopBean modeSettingLoopBean, OnCloseListener listener) {
        super(context,  R.style.dialog);

        this.mContext = context;
        this.modeSettingLoopBean = modeSettingLoopBean;
        this.listener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_mode_custom_loop);
        setCanceledOnTouchOutside(false);
        initView();
    }

    private void initView() {

        loopContentView = (ModeSettingItemLoopContentView) findViewById(R.id.view_mode_loop_content);

        submitTxt = (TextView) findViewById(R.id.submit);
        submitTxt.setOnClickListener(this);
        cancelTxt = (TextView) findViewById(R.id.cancel);
        cancelTxt.setOnClickListener(this);

        loopContentView.setSettingLoopBean(modeSettingLoopBean);
        loopContentView.setOnModeSettingItemLoopContentViewListner(new ModeSettingItemLoopContentView.OnModeSettingItemLoopContentViewListner() {
            @Override
            public void onModeSettingItemValueClick(ModeSettingBean.ModeSettingLoopBean loopBean, ModeSettingBean.ModeSettingItemBean itemBean, ModeSettingBean.ModeSettingItemTag itemTag) {
                //隐藏loopDiaolog
                hide();
                showItemValuePopView(loopBean, itemBean, itemTag);
            }
        });
    }

    private void showItemValuePopView(ModeSettingBean.ModeSettingLoopBean loopBean, ModeSettingBean.ModeSettingItemBean itemBean, ModeSettingBean.ModeSettingItemTag itemTag) {
        if (itemTag == ModeSettingBean.ModeSettingItemTag.ModeSettingItemTagLoopCount
                || itemTag == ModeSettingBean.ModeSettingItemTag.ModeSettingItemTagLoopTime) {
            List<ModeSettingBean.ModeSettingItemBean> itemBeans = new ArrayList<>();
            itemBeans.add(loopBean.getLoopCountItemBean());
            itemBeans.add(loopBean.getLoopTimeItemBean());

            List<String> itemMsgs = new ArrayList<>();
            itemMsgs.add(loopBean.getLoopCountItemBean().getDescWithItemTag(ModeSettingBean.ModeSettingItemTag.ModeSettingItemTagLoopCount));
            itemMsgs.add(loopBean.getLoopTimeItemBean().getDescWithItemTag(ModeSettingBean.ModeSettingItemTag.ModeSettingItemTagLoopTime));

            int index = itemTag == ModeSettingBean.ModeSettingItemTag.ModeSettingItemTagLoopCount ? 0 : 1;
            ModeSettingItemValuePopView popView = new ModeSettingItemValuePopView(mContext, itemBeans, itemMsgs, index, new ModeSettingItemValuePopView.OnSettingItemValuePopViewListner() {
                @Override
                public void onValueFinish(ModeSettingBean.ModeSettingItemBean itemBean, int index) {
                    show();
                    loopBean.setLoopType(index);
                    reloadData();
                }

                @Override
                public void onValueError(String error) {
                    show();
                    ToastUtil.showToast(mContext, error);
                }

                @Override
                public void onCancelBtnClick() {
                    show();
                }
            });
            popView.show();

        } else {
            String itemMsg = itemBean.getDescWithItemTag(itemTag);
            ModeSettingItemValuePopView popView = new ModeSettingItemValuePopView(mContext, itemBean, itemMsg, new ModeSettingItemValuePopView.OnSettingItemValuePopViewListner() {
                @Override
                public void onValueFinish(ModeSettingBean.ModeSettingItemBean itemBean, int index) {
                    show();
                    reloadData();
                }

                @Override
                public void onValueError(String error) {
                    show();
                    ToastUtil.showToast(mContext, error);
                }

                @Override
                public void onCancelBtnClick() {
                    show();
                }
            });
            popView.show();
        }
    }

    private void reloadData() {
        loopContentView.setSettingLoopBean(modeSettingLoopBean);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel:
                if (listener != null) {
                    listener.onClick(this, false);
                }
                this.dismiss();
                break;
            case R.id.submit:
                if (listener != null) {
                    listener.onClick(this, true);
                }
                break;
        }
    }

    public interface OnCloseListener {
        void onClick(Dialog dialog, boolean confirm);
    }

}
